var app=sm("do_App");
var page=sm("do_Page");
var nf = sm("do_Notification");
var config=require("config");
var cache = sm("do_DataCache");
var ListViewflow = mm("do_ListData");
var global=sm("do_Global");
var storage=sm("do_Storage");

var do_back=ui("do_back");
var do_rebut=ui("do_rebut");
var do_pass=ui("do_pass");
var do_ALayout_root=ui("do_ALayout_root");
var do_ListView_flow=ui("do_ListView_flow");
var do_ScrollView=ui("do_ScrollView");
var do_Sumbit=ui("do_Sumbit");


//用于等待登录的过程
do_ALayout_root.add("loadingUI", "source://view/loadingUI.ui", 0, 0);
var loadingUI = ui("loadingUI");
//初始化隐藏遮盖
loadingUI.visible = true;

do_ALayout_root.add("DialogUI", "source://view/approve/approveDialog.ui", 0, 0);
var DialogUI = ui("DialogUI");
DialogUI.visible = false;
var sumbitBtn="";
var billId="";

var userinfo=cache.loadData("userinfo");
do_back.on("touch",function(){
	app.closePage();
});

page.on("back",function(){
	if(DialogUI.visible==true){
		var nowdate=new Date();
		var olddate=nowdate.getTime();
		app.fire("tologin",{times:olddate,pagesize:3});
		DialogUI.visible=false;
		storage.deleteFile("data://"+billId+".txt");
	}
	else{
		app.closePage();
	}	
});

page.on("loaded", function(data, e) {
	var nowdate=new Date();
	var olddate=nowdate.getTime();
	app.fire("tologin",{times:olddate,pagesize:3});
	var pagedata=this.getData();
	billId=pagedata.billId;
	if(pagedata.approveType==0){
		do_ScrollView.height=1145;
		do_Sumbit.visible=true;
		do_ScrollView.redraw();
	}
	
	var http = mm("do_Http");
	http.method = "POST"; 
	http.timeout = 30000; 
	http.contentType = "application/json"; 
	http.url = config.baseUrl+"approveDetail"; 
	http.body = JSON.stringify({userId:userinfo.userid,billId:billId,usernum:userinfo.usernum,userId:userinfo.userid}); 
	http.on("success", function(data) {	
		loadingUI.visible = false;
		if(data.result==1 && data.data){
			do_ALayout_root.tag=data.data.billId;
			ui("do_calloutCompany").text=(data.data.calloutcompany!=null)?data.data.calloutcompany:"";
			ui("do_calloutMomey").text=(data.data.calloutmomey!=null)?parseFloat(data.data.calloutmomey).toFixed(2):"";
			//	ui("do_calloutbank").text=(data.data.calloutbank!=null)?data.data.calloutbank:"";
		//	ui("do_calloutaccount").text=(data.data.calloutaccount!=null)?data.data.calloutaccount:"";
			ui("do_cyx1").text=(data.data.zyx1!=null)?data.data.zyx1:"";
			ui("do_callinCompany").text=(data.data.callincompany!=null)?data.data.callincompany:"";
			ui("do_callinMomey").text=(data.data.callinmomey!=null)?parseFloat(data.data.callinmomey).toFixed(2):"";
		//	ui("do_callinbank").text=(data.data.callinbank!=null)?data.data.callinbank:"";
		//	ui("do_callinaccount").text=(data.data.callinaccount!=null)?data.data.callinaccount:"";
			ui("do_RichLabel").text=(data.data.zyx3!=null)?data.data.zyx3:"";
			var approveProcessList=data.data.approveProcessList;
			if(approveProcessList.length>0){
				approveProcessList.forEach(function(ele, i) {
					ele.user_name1=(ele.user_name1!=null)?ele.user_name1:"";
					ele.dealdate=(ele.dealdate!=null)?ele.dealdate:"";
					ele.checknote=(ele.checknote!=null)?ele.checknote:"";
					ele.user_name2=(ele.user_name2!=null)?ele.user_name2:"";
					//approveProcessList[0].approvestatus=0;
					switch (ele.approvestatus) {
					case 1:
						ele.approvetext="通过";
						ele.templates=0;
						break;	
					case 0:
						ele.approvetext="未审核";
						ele.templates=0;
						break;
					case 4:
						ele.approvetext="作废";
						ele.templates=0;
						break;
					default:
						ele.approvetext="审批完成";
						ele.templates=1;
						break;
					}					
				})	
				
				approveProcessList.unshift({user_name2:approveProcessList[0].user_name1,dealdate:approveProcessList[0].senddate,approvetext:"提交"});
				do_ListView_flow.height=(approveProcessList.length)*194;
				do_ListView_flow.redraw();
				ListViewflow.addData(approveProcessList);
				do_ListView_flow.bindItems(ListViewflow);
			}
			
			do_rebut.on("touchDown", function(){
				do_rebut.bgColor = "EF1212FF";	
			}).on("touchUp",function(){
				do_rebut.bgColor = "F1312FFF";
			}).on("touch",function(){
				var nowdate=new Date();
				var olddate=nowdate.getTime();
				app.fire("tologin",{times:olddate,pagesize:3});
				DialogUI.visible = true;
				page.fire("submitOpinion",{text:"驳回",billid:billId});
				sumbitBtn=2;
			});
			do_pass.on("touchDown", function(){
				do_pass.bgColor = "209A1DFF";	
			}).on("touchUp",function(){
				do_pass.bgColor = "28C324FF";
			}).on("touch",function(){
				var nowdate=new Date();
				var olddate=nowdate.getTime();
				app.fire("tologin",{times:olddate,pagesize:3});
				DialogUI.visible = true;
				page.fire("submitOpinion",{text:"通过",billid:billId});
				sumbitBtn=1;
			});
		}else if(data.result==3045){	
			 app.closePage({data:"帐号可能被其它终端登录，请重新登录", animationType:"", layer:3});	
			 cache.saveData("userinfo", "");
		}
		else{
			nf.toast(data.msg || "失败");
		}		
		
	});
	http.on("fail", function(data) {
		loadingUI.visible = false;
		do_ListView_flow.rebound();
		nf.toast(data.msg || "网络超时");
	});
	http.request();
});
page.on("DialogUIClose",function(data){
	DialogUI.visible=false;
	var nowdate=new Date();
	var olddate=nowdate.getTime();
	app.fire("tologin",{times:olddate,pagesize:3});
	if(data!="false"){
		loadingUI.visible = true;
		do_rebut.enabled=false;
		do_pass.enabled=false;
		loadingUI.visible = true;
		var http = mm("do_Http");	
		http.method = "POST"; 
		http.timeout = 30000; 
		http.contentType = "application/json"; 
		http.url = config.baseUrl+"approveSubmit"; 
	    http.body = JSON.stringify({billId:billId,submit:sumbitBtn,submitOpinion:data,usernum:userinfo.usernum,userId:userinfo.userid}); 
	    http.on("success", function(data) {	
	    	app.fire("updateIndex");
	    	storage.deleteFile("data://"+billId+".txt");
	    	loadingUI.visible = false;
	    	do_rebut.enabled=true;
	    	do_pass.enabled=true;
	    	loadingUI.visible = false;
	    	if( data.result==1){	
				app.closePage({data:{mark:true,toast:"审批成功"}});		
		
			}
//			else if(data.msg.indexOf("当前操作人没有审批任务}")){
//				nf.toast("此单据可能已被审批");
//				app.closePage({data:true});
//			}
			else if(data.result==3045){	
				 app.closePage({data:"帐号可能被其它终端登录，请重新登录", animationType:"", layer:3});	
				 cache.saveData("userinfo", "");
			}
			else{
				nf.toast(data.msg || "失败");
			}
		});
		http.on("fail", function(data) {
			loadingUI.visible = false;
			do_rebut.enabled=true;
	    	do_pass.enabled=true;
			nf.toast(data.msg || "网络超时");
		});
		http.request();
	}
});
do_ScrollView.on("scroll",function(){
	var nowdate=new Date();
	var olddate=nowdate.getTime();
	app.fire("tologin",{times:olddate,pagesize:3});
});